﻿using System;
using System.Web;
using TCL.EO;
using System.Collections.Generic;

namespace TCL.FUNC
{
    public class SystemEx
    {
        /// <summary>
        /// get Root Path current
        /// </summary>
        /// <returns></returns>
        public static String getRootPath()
        {
            if (SystemEx.getAppSettings("NMT_Platform").Equals("webform"))
                return HttpRuntime.AppDomainAppPath;
            else if (SystemEx.getAppSettings("NMT_Platform").Equals("winform"))
                return AppDomain.CurrentDomain.BaseDirectory;
            else if (SystemEx.getAppSettings("NMT_Platform").Equals("winsv"))
                return AppDomain.CurrentDomain.BaseDirectory;
            else
                return String.Empty;
        }

        /// <summary>
        /// get App Setting in app.config or web.config
        /// </summary>
        /// <param name="sKey"></param>
        /// <param name="sDefault"></param>
        /// <returns></returns>
        public static String getAppSettings(String sKey, String sDefault = "")
        {
            if (System.Configuration.ConfigurationManager.AppSettings["NMT_isDebug"] != null && System.Configuration.ConfigurationManager.AppSettings[sKey + "_Debug"] != null && StringEx.Trim(System.Configuration.ConfigurationManager.AppSettings["NMT_isDebug"]).Equals("1"))
                sKey = sKey + "_Debug";
            if (System.Configuration.ConfigurationManager.AppSettings[sKey] != null)
                return System.Configuration.ConfigurationManager.AppSettings[sKey];
            else
                return sDefault;
        }

        /// <summary>
        /// OS, PATH, COMPUTER_NAME, UserName
        /// </summary>
        /// <returns></returns>
        public static List<ConnectionStringEO> lstConn;
        public static String getConnectionString()
        {
            String sRet = "";
            String strCSS = SystemEx.getAppSettings("NMT_CSS", "");
            if (!String.IsNullOrEmpty(strCSS))
            {
                strCSS = SecurityEx.DecryptString(strCSS, SecurityEx.Base64Encode(Environment.MachineName));
                String[] arrCon = StringEx.Trim(strCSS).Split('|');
                if (arrCon.Length >= 5)
                {
                    sRet = "Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}";
                    sRet = String.Format(sRet, arrCon[0], arrCon[1], arrCon[2], arrCon[3]);
                }
            }
            return sRet;
        }
    }
}
